import 'package:flutter/material.dart';
import 'package:flutter_mark_trip/dao/travel_tab_dao.dart';
import 'package:flutter_mark_trip/model/travel_tab_model.dart';
import 'package:flutter_mark_trip/pages/Travel_Tab_Page.dart';

///* 旅拍
class TravelPage extends StatefulWidget {
  @override
  _TravelPageState createState() => _TravelPageState();
}

class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin {
  TabController _tabController;
  List<TravelTab> tabs = [];
  TravelTabModel _travelTabModel;

  @override
  void initState() {
    _tabController = TabController(length: 0, vsync: this);
    TravelTabDao.fetch().then((TravelTabModel tabModel) {
      //fix tab label 空白问题
      _tabController = TabController(length: tabModel.tabs.length, vsync: this);
      setState(() {
        tabs = tabModel.tabs;
        _travelTabModel = tabModel;
      });
    }).catchError((e) {
      print(e);
    });
    super.initState();
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            padding: EdgeInsets.only(top: 30),
            child: TabBar(
                labelColor: Colors.black,
                isScrollable: true,
                controller: _tabController,
                labelPadding: EdgeInsets.fromLTRB(15, 0, 15, 5),
                indicator: UnderlineTabIndicator(
                    borderSide: BorderSide(
                      color: Color(0xff2fcfbb),
                      width: 3,
                    ),
                    insets: EdgeInsets.only(bottom: 10)),
                tabs: tabs.map((TravelTab tab) {
                  return Tab(
                    child: Text(
                      tab.labelName,
                      textAlign: TextAlign.center,
                      style: TextStyle(fontSize: 14),
                    ),
                  );
                }).toList()),
          ),
          Flexible(
            child: TabBarView(
                controller: _tabController,
                children: tabs.map((TravelTab tab) {
                  return TravelTabPage(
                    travelUrl: _travelTabModel.url,
                    params: _travelTabModel.params,
                    groupChannelCode: tab.groupChannelCode,
                  );
                }).toList()),
          ),
        ],
      ),
    );
  }
}
